<?php


namespace App\Admin\Controllers;


use SmallRuralDog\Admin\Components\Grid\Image;
use SmallRuralDog\Admin\Components\Grid\Route;
use SmallRuralDog\Admin\Components\Grid\Tag;
use SmallRuralDog\Admin\Components\Widgets\Button;
use SmallRuralDog\Admin\Components\Widgets\Dialog;
use SmallRuralDog\Admin\Components\Widgets\Markdown;
use SmallRuralDog\Admin\Controllers\AdminController;
use SmallRuralDog\Admin\Grid;
use SmallRuralDog\Admin\Layout\Content;

class Top250Controller extends AdminController
{

    public function grid()
    {
        $page = request('page', 1);
        $pre = 10;

        $grid = new Grid();

        if ($grid->isGetData()) {

            $start = ($page - 1) * $pre;

            $data = \Cache::rememberForever("top250-$start-$pre", function () use ($pre, $start) {
                return \Http::get("https://api.douban.com/v2/movie/top250?start= $start&count=$pre&apikey=0df993c66c0c636e29ecbb5344252a4a")->object();
            });
            $lsat_page = (int)($data->total / $pre);
            $grid->customData($data->subjects, $page, $pre, $lsat_page, $data->total);
        }

        $grid->autoHeight();


        $grid->column('title')->width(180);

        $grid->column('images.small', 'Image')->component(Image::make()->preview()->size(270 / 5, 400 / 5))->width(120);
        $grid->column('year')->width(80);
        $grid->column('rating.average', 'Rating')->width(80);
        $grid->column('directors.name', 'Directors')->component(Tag::make());
        $grid->column('casts.name', 'Casts')->component(Tag::make());
        $grid->column('genres', 'Casts')->component(Tag::make()->type('success'));

        $grid->batchActions(function (Grid\BatchActions $actions) {
            $actions->hideDeleteAction();
        });
        $grid->ref("top250");
        $grid->toolbars(function (Grid\Toolbars $toolbars) {
            $toolbars->createButton()->disabled();
            $js = <<<JS
self.attrs.type='success'
self.\$message.success("获取成功，请在浏览器调试器查看打印数据")
console.log(ref.selectionRows)
JS;
            $toolbars->addLeft(Button::make("动态获取已选择的项目")->refData("top250", $js));
            $js = <<<JS
ref.\$refs.top250.clearSelection();
JS;

            $toolbars->addLeft(Button::make("调用表格事件，清除全选")->refData("top250", $js)->className('ml-10'));

            $js = <<<JS
ref.\$bus.emit("tableReload");
JS;

            $toolbars->addLeft(Button::make("手动发送emit")->refData("top250", $js)->className('ml-10'));

            $toolbars->addLeft(Button::make("表格交互")->ref('gButton')->className('ml-10')->dialog(function (Dialog $dialog) {
                $dialog->width('80%');
                $dialog->ref("gDialog")->showClose(false);
                $dialog->slot(function (Content $content) {
                    $this->dialogGrid($content);
                });
                $dialog->title("表格交互");
            }));
            $js = <<<JS
let table = ref.\$refs.top250
table.stripe=false;
table['rowClassName'] = ({row, rowIndex})=>{
        if (rowIndex === 1) {
          return 'warning-row';
        } else if (rowIndex === 3) {
          return 'success-row';
        }
        return '';
}
JS;


            $toolbars->addLeft(Button::make("设置表格row-class-name")->refData("top250", $js)->className('ml-10'));
        });

        $grid->top(function (Content $content) {
            $content->row($this->code())->className('mb-10');
        });

        $grid->selection();
        $grid->actions(function (Grid\Actions $actions){
            $actions->hideEditAction();
            $actions->hideDeleteAction();
            $title = $actions->getRow()->title;
            $actions->add(Grid\Actions\ActionButton::make("操作：$title"));
        });

        return $grid;
    }

    protected function dialogGrid(Content $content)
    {
        $page = request('page', 1);
        $pre = 10;

        $grid = new Grid();
        $grid->dataUrl(route('top250-dialogGrid'));

        if ($grid->isGetData()) {
            $data = \Http::get("https://api.douban.com/v2/movie/top250?start=" . (($page - 1) * $pre) . "&count=$pre&apikey=0df993c66c0c636e29ecbb5344252a4a")->object();
            $lsat_page = (int)($data->total / $pre);
            $grid->customData($data->subjects, $page, $pre, $lsat_page, $data->total);
        }


        $grid->column('title')->width(180);

        $grid->column('images.small', 'Image')->component(Image::make()->size(27, 40))->width(80);
        $grid->column('year')->width(80);
        $grid->column('rating.average', 'Rating')->width(80);
        $grid->column('directors.name', 'Directors')->component(Tag::make());
        $grid->column('casts.name', 'Casts')->component(Tag::make());
        $grid->column('genres', 'Casts')->component(Tag::make()->type('success'));
        $grid->hideActions()->selection();
        $grid->toolbars(function (Grid\Toolbars $toolbars) {
            $toolbars->hide();
        });
        $grid->batchActions(function (Grid\BatchActions $actions) {
            $actions->hideDeleteAction();
        });
        if ($grid->isGetData()) {
            return $grid;
        }
        $content->row($grid);


        $grid->ref("dialogGrid");

        $grid->bottom(function (Content $content) {


            $js = <<<JS

ref.\$bus.emit("gButton",{data:"ref.attrs.content = '一共选择了：'+self.selectionRows.length;ref.dialogTableVisible=false;ref.attrs.type='success';",self:ref})

JS;

            $content->row(Button::make("将选中的匹配到页面")->refData("dialogGrid", $js))->className('p-10');
        });


        return $content;
    }

    protected function code($name = "查看源代码", $ref = "codeButton")
    {
        return Button::make($name)->ref($ref)->dialog(function (Dialog $dialog) use ($name) {
            $dialog->width('80%')->title($name);
            $dialog->slot(function (Content $content) {
                $code = "```php\n";
                $code .= file_get_contents(admin_path("Controllers/Top250Controller.php"));
                $code .= "\n```";

                $content->body(Markdown::make($code)->style("height:60vh;"));
            });
        });
    }

}
